package cn.dai.servlet;

import cn.dai.dao.RoleDaoImpl;
import cn.dai.pojo.Role;
import cn.dai.service.RoleService;
import cn.dai.service.RoleServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ArkD42
 * @file RBAC
 * @create 2020 - 06 - 11 - 21:18
 */
@WebServlet("/role")
public class RoleServlet extends BaseServlet{

    RoleService roleService = new RoleServiceImpl(new RoleDaoImpl());

    /**
     * http://localhost:8080/RBAC/role?act=roleList
     * @param request
     * @param response
     * @return
     */
    public String roleList(HttpServletRequest request, HttpServletResponse response) {
        List<Role> roleList = roleService.getAllRole();
        request.setAttribute("roleList",roleList);
        return "/WEB-INF/admin/role/roleList.jsp";
    }

    /**
     * /role?act=toAddRole
     * @param request
     * @param response
     * @return
     */
    public String toAddRole(HttpServletRequest request, HttpServletResponse response){
        return "/WEB-INF/admin/role/roleAdd.jsp";
    }

    /**
     * /role?act=addRole
     * @param request
     * @param response
     */
    public void addRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Role role = new Role();
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        role.setR_name(name);
        role.setR_description(description);
        int i = roleService.addRole(role);
        System.out.println("添加用户影响结果：" + i);
        response.sendRedirect(request.getContextPath() + "/role?act=roleList");
    }

    public String toEditRole(HttpServletRequest request,HttpServletResponse response){

        String id = request.getParameter("id");

        Role role = roleService.getRoleById(Integer.valueOf(id));

        request.setAttribute("role",role);

        return "/WEB-INF/admin/role/roleEdit.jsp";
    }



}
